<style rel="stylesheet/scss" lang="scss">


</style>

<template>
    <div>
        <div class="info_flow_content" @click="bodyEvent">
            <div class="info_ad_search">
                <div class="info_a_s_content">
                    <div class="customerProCitySearch">
                        <div class="block">
                            <span class="demonstration">选择日期</span>
                            <el-date-picker
                                v-model="dateValue"
                                type="daterange"
                                align="right"
                                placeholder="选择日期范围"
                                @change="dateChange"
                                format="yyyy/MM/dd"
                                :picker-options="$store.state.pickerOptions2">
                            </el-date-picker>
                        </div><!--
					  -->
                        <el-button class="page_d_s_button" @click="pageInfo()">查询</el-button>
                        <div class="dateBut" @click="importData">
                            <el-button type="primary">下载报表</el-button>
                        </div>
                    </div>
                </div>
            </div>
            <!--<div class="finance_info">-->
                <!--<div class="f_i_date">-->
                    <!--<span class="f_i_date_title">账号余额<em style="margin-left:10px;">100000</em></span>-->
                    <!--<span class="f_i_date_title">花费总额<em style="margin-left:10px;">123098</em></span>-->
                <!--</div>-->

            <!--</div>-->
            <div class="info_ad_table">
                <div class="page_d_title">
                    <!--<i style="visibility:hidden;"></i><span></span>-->
                    <span class="f_i_date_title">账号余额<em style="margin-left:10px;">{{amountTotal}}</em></span>
                    <span class="f_i_date_title">花费总额<em style="margin-left:10px;">{{costTotal}}</em></span>
                    <div class="tableVisibleMenu">
                        <p class="visibleMenuBut" @click="changeCheckStatus($event)"></p>
                        <div class="visibleMenuSelect" v-if="visibleMenuSelectStatus" @click="preventDefault($event)">
                            <el-checkbox-group
                                v-model="checkedCities"
                                :min="1"
                                @change="checkedChange">
                                <el-checkbox v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox>
                            </el-checkbox-group>
                        </div>
                    </div>
                </div>
                <div class="tableDetail">
                    <el-table
                        :data="tableData"
                        border
                        stripe
                        style="width: 100%"
                    >
                        <el-table-column
                            v-for="(c, i) in table_column"
                            :key="i"
                            :fixed="c.fixed"
                            :sortable="c.sortable"
                            :prop="c.prop"
                            :label="c.label"
                            :formatter="c.format"
                            width=""
                            v-if="c.visible">
                        </el-table-column>
                    </el-table>
                </div>
            </div>
            <div class="con_pager">
                <div class="block">
                    <!--<span class="demonstration">直接前往</span>-->
                    <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page.sync="page.currentPage"
                        :page-sizes="page.sizes"
                        :page-size="page.limit"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="page.count">
                    </el-pagination>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                checked: true,
                billStatus: false,
                visibleMenuSelectStatus: false,
                checkedCities: ['日期', '操作类型', '操作金额', '备注'],
                cities: ['日期', '操作类型', '操作金额', '备注'],
                citiesFilterVal: ['date', 'type', 'amount', 'notes'],
                table_column: [
                    {fixed: false, prop: 'date', label: '日期', sortable: true, visible: true},
                    {fixed: false, prop: 'type', label: '操作类型', sortable: false, visible: true},
                    {fixed: false, prop: 'amount', label: '操作金额', sortable: false, visible: true},
                    {fixed: false, prop: 'notes', label: '备注', sortable: false, visible: true},
                ],
                page: {
                    sizes: [10, 20, 30, 50,100,500],
                    offset: 1,
                    limit: 10,
                    count: 0,
                    currentPage: 1,
                    landpageName: ''
                },
                menu_nav: ['广告主', '财务中心'],
                tableData: [],
                billNo: '',
                dateValue: [new Date(new Date().getTime()-7*24*60*60*1000),new Date()],
                params: {
                    type: '1',
                    val: '',
                    startDate: '',
                    endDate: ''
                },
                amountTotal:'',
                costTotal:'',
                isFirstEnter:false, // 是否第一次进入，默认false
            };
        },
        activated() {
            if(!this.$route.meta.isBack|| this.isFirstEnter){
                // 如果isBack是false，表明需要获取新数据，否则就不再请求，直接使用缓存的数据
                this.dateChange();
                this.pageInfo();
            }
            // 恢复成默认的false，避免isBack一直是true，导致下次无法获取数据
            this.$route.meta.isBack=false;
            // 恢复成默认的false，避免isBack一直是true，导致每次都获取新数据
            this.isFirstEnter=false;
        },
        created() {
            this.isFirstEnter=true;
        },
        methods: {
            pageInfo() {
                if (!(this.dateValue&&this.dateValue[0])) {
                    this.$message({
                        type: 'info',
                        message: '请选择日期'
                    });
                    return false;
                }
                /*列表*/
                this.$http({
                    method: 'get',
                    url: this.apiUrl.adGetdeposit,
                    params: {
                        offset: this.page.offset,
                        limit: this.page.limit,
                        sdate: this.params.startDate,
                        edate: this.params.endDate,
                    }
                }).then(res => {
//                    console.log(res);
                    if (res.status == 200) {
                        var data = res.data;
                        this.page.count = data.totalNums;
                        this.amountTotal=data.amountTotal;
                        this.costTotal=data.costTotal;
                        this.tableData = data.list;
                    } else {
                    }
                }).catch(error => {
                    console.log(error);
                });
            },
            handleCurrentChange(val) {
                this.page.offset = val;
                this.pageInfo();
            },
            handleSizeChange(val) {
                this.page.limit = val;
                this.pageInfo();
            },
            checkedChange() {
                for (let item of this.table_column) {
                    var index = this.checkedCities.indexOf(item.label);
                    index >= 0 ? item.visible = true : item.visible = false;
                }
            },
            changeCheckStatus(e) {
                e.stopPropagation();
                this.visibleMenuSelectStatus = !this.visibleMenuSelectStatus;
            },
            exitBill() {
                this.billStatus = false;
            },
            bodyEvent() {
                this.visibleMenuSelectStatus = false;
//                this.billStatus = false;
            },
            preventDefault(e) {
                e.stopPropagation();
            },
            dateChange() {
                this.params.startDate = this.formatDateTime(this.dateValue[0], 'y-m-d');
                this.params.endDate = this.formatDateTime(this.dateValue[1], 'y-m-d');
            },
            importData() {
                if (this.tableData.length == 0) {
                    this.$message({
                        type: 'info',
                        message: '请查询数据'
                    });
                    return false;
                }
                let tHeader = this.cities;
                let filterVal = this.citiesFilterVal;
                let importData = this.tableData;
                let fileName = [this.params.startDate,this.params.endDate, '财务中心'].join('_');
                this.handleDownload(tHeader, filterVal, importData, fileName);
            },
        }
    };
</script>
